package com.ykdl.member.kid.fittool;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;

import android.app.ProgressDialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.ykdl.member.kid.R;
import com.ykdl.member.dictionary.DictionaryDataManager;
import com.ykdl.member.http.Network;
import com.ykdl.member.kid.beans.BreastFeeding;
import com.ykdl.member.kid.beans.StatuResult;
import com.ykdl.member.kid.util.AssertTool;
import com.ykdl.member.kid.util.CalendarUtil;
import com.ykdl.member.kid.widget.RefreshListView;
import com.ykdl.member.views.ArrayWheelAdapter;
import com.ykdl.member.views.DateWheelAdapter;
import com.ykdl.member.views.NumericWheelAdapter;
import com.ykdl.member.views.WheelView;

public class AddBreastFeeding implements OnClickListener{
	
	private Context context;
	private ViewGroup group;
	private RefreshListView mListView;
	private TextView mTxtTitle;
	private TextView mTxtDuration, mTxtVomits, mTxtTime;
	private Button mBtnClose, mBtnBack, mBtnNext, mBtnComplate;
	private View mLayerBreastDuration, mLayerBreastTime, mLayerBreastVomits;
	private WheelView mWheelView1, mWheelView2,mWheelView3,mWheelView4,mWheelView5,mWheelView6,mWheelView7;
	
	private List<String> breast_feed;
	private BreastFeeding breast = new BreastFeeding();
	private DateWheelAdapter mDateAdapter;
	private CalendarUtil mCalendarUtil;
	private ProgressDialog mProgressDialog;
	
	private OnRefreshListListener mListListener;
	
	public AddBreastFeeding(Context context, ViewGroup group, RefreshListView listview){
		this.context = context;
		this.group = group;
		this.mListView = listview;
		this.mListListener = (OnRefreshListListener) context;
		View view = ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.layout_feed_tool_breask, group);
		
		mCalendarUtil = new CalendarUtil();
		
		
		mTxtDuration = (TextView) view.findViewById(R.id.txtDuration);
		mTxtVomits = (TextView) view.findViewById(R.id.txtVomits);
		mTxtTime = (TextView) view.findViewById(R.id.txtTime);
		
		mTxtTitle = (TextView) view.findViewById(R.id.txtTitle);
		mTxtTitle.setText("左侧/右侧时长");
		
		mBtnClose = (Button) view.findViewById(R.id.btnClose);
		mBtnClose.setOnClickListener(this);
		mBtnBack = (Button) view.findViewById(R.id.btnBack);
		mBtnBack.setOnClickListener(this);
		mBtnNext = (Button) view.findViewById(R.id.btnNext);
		mBtnNext.setOnClickListener(this);
		mBtnComplate = (Button) view.findViewById(R.id.btnComplate);
		mBtnComplate.setOnClickListener(this);
		
		
		mLayerBreastDuration = view.findViewById(R.id.layerBreastDuration);
		breast_feed = DictionaryDataManager.getInstance().getParentDataList(context, AssertTool.BREAST_FEED);
		mWheelView1 = (WheelView) view.findViewById(R.id.wheelView1);
		String[] items = new String[breast_feed.size()];
		breast_feed.toArray(items);
		ArrayWheelAdapter<String> adapter = new ArrayWheelAdapter<String>(items);
		mWheelView1.setAdapter(adapter);
		mWheelView1.getCurrentItem();
		mWheelView2 = (WheelView) view.findViewById(R.id.wheelView2);
		mWheelView2.setAdapter(new NumericWheelAdapter(1, 60));
		mWheelView2.setLabel("分钟");
		
		mLayerBreastVomits = view.findViewById(R.id.layerBreastVomits);
		mWheelView7 = (WheelView) view.findViewById(R.id.wheelView7);
		String[] vomits = {"无吐奶", "有吐奶"};
		mWheelView7.setAdapter(new ArrayWheelAdapter<String>(vomits));
		
		mLayerBreastTime = view.findViewById(R.id.layerBreastTime);
		mWheelView3 = (WheelView) view.findViewById(R.id.wheelView3);
		String[] strs = {"开吃时间", "吃完时间"};
		mWheelView3.setAdapter(new ArrayWheelAdapter<String>(strs));
		mWheelView4 = (WheelView) view.findViewById(R.id.wheelView4);
		Calendar end = Calendar.getInstance();
		long endtime = end.getTimeInMillis();
		mDateAdapter = new DateWheelAdapter(endtime-(24*60*60*1000*14), endtime);
		mWheelView4.setAdapter(mDateAdapter);
		mWheelView5 = (WheelView) view.findViewById(R.id.wheelView5);
		mWheelView5.setAdapter(new NumericWheelAdapter(0, 23));
		mWheelView6 = (WheelView) view.findViewById(R.id.wheelView6);
		mWheelView6.setAdapter(new NumericWheelAdapter(0, 59));
		
	}
	

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnClose:
			group.removeAllViews();
			group.setVisibility(View.GONE);
			mListView.setEnabled(true);
			break;
		case R.id.btnBack:
			if (mLayerBreastTime.getVisibility() == View.VISIBLE) {
				mLayerBreastTime.setVisibility(View.GONE);
				mLayerBreastVomits.setVisibility(View.VISIBLE);
				mBtnComplate.setVisibility(View.GONE);
				mBtnNext.setVisibility(View.VISIBLE);
				mTxtTitle.setText("有无吐奶");
			} else if (mLayerBreastVomits.getVisibility() == View.VISIBLE) {
				mLayerBreastDuration.setVisibility(View.VISIBLE);
				mLayerBreastVomits.setVisibility(View.GONE);
				mBtnBack.setVisibility(View.GONE);
				mBtnClose.setVisibility(View.VISIBLE);
				mTxtTitle.setText("开始/吃完时间");
			}
			break;
		case R.id.btnNext:
			if (mLayerBreastDuration.getVisibility() == View.VISIBLE) {
				int curItem = mWheelView1.getCurrentItem();
				breast.setBreast_type(curItem+1);
				int duration = mWheelView2.getCurrentItem()+1;
				breast.setDuration(duration);
				mTxtDuration.setText(breast_feed.get(mWheelView1.getCurrentItem())+duration+"分钟");
				mLayerBreastDuration.setVisibility(View.GONE);
				mBtnClose.setVisibility(View.GONE);
				mBtnBack.setVisibility(View.VISIBLE);
				mLayerBreastVomits.setVisibility(View.VISIBLE);
				mTxtTitle.setText("有无吐奶");
			} else if (mLayerBreastVomits.getVisibility() == View.VISIBLE) {
				breast.setVomits(mWheelView7.getCurrentItem());
				mTxtVomits.setText(breast.getVomits()==0?"无吐奶":"有吐奶");
				mLayerBreastVomits.setVisibility(View.GONE);
				mBtnClose.setVisibility(View.GONE);
				mBtnBack.setVisibility(View.VISIBLE);
				mBtnNext.setVisibility(View.INVISIBLE);
				mBtnComplate.setVisibility(View.VISIBLE);
				mLayerBreastTime.setVisibility(View.VISIBLE);
				mTxtTitle.setText("开始/吃完时间");
			}
			break;
		case R.id.btnComplate:
			if (mLayerBreastTime.getVisibility() == View.VISIBLE) {
				int item3 = mWheelView3.getCurrentItem();
				String month, day;
				String strDate = mDateAdapter.getItem(mWheelView4.getCurrentItem());
				if (strDate.equals("今天")) {
					String[] str = new SimpleDateFormat("MM-dd").format(new Date()).split("-");
					month = str[0];
					day = str[1];
				} else {
					String[] str = strDate.split("月");
					month = str[0];
					day = str[1].replace("日", "");
				}
				int hour = mWheelView5.getCurrentItem();
				int minute = mWheelView6.getCurrentItem();
				long time = mCalendarUtil.getLongByString(month, day, String.valueOf(hour), String.valueOf(minute));
				if (item3 == 0) {
					breast.setBegin_time(time/1000);
					breast.setEnd_time(time/1000+breast.getDuration()*60);
				} else {
					breast.setEnd_time(time/1000);
					breast.setBegin_time(time/1000 - breast.getDuration()*60);
				}
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				mTxtTime.setText(sdf.format(new Date(time)));
				submitData(breast);
			}
			break;
		default:
			break;
		}
	}
	
	private void submitData(BreastFeeding breast) {
		mProgressDialog = ProgressDialog.show(context, null, "正在提交中...");
		Network.getInstance().addFeedBean(breast, new AddDataTag());
	}
	
	private class AddDataTag implements ITag<StatuResult>{

		@Override
		public void refreshUI(StatuResult t, DataState ds) {
			mProgressDialog.dismiss();
			if (t != null) {
				// 是否刷新视图 ？？
				group.removeAllViews();
				group.setVisibility(View.GONE);
				mListView.setEnabled(true);
				mListListener.refreshList();
			} else {
				Toast.makeText(context, "添加数据失败", 1).show();
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			mProgressDialog.dismiss();
			if (TextUtils.isEmpty(parseError.getErrMsg())) {
				Toast.makeText(context, "添加数据失败", 1).show();
				return true;
			} else {
				return false;
			}
		}

		@Override
		public void refreshCancel() {
			
		}
		
	}
	
}
